<!--
 * @Author: 王鹏 2911783749@qq.com
 * @Date: 2022-10-20 13:58:17
 * @LastEditors: 王鹏 2911783749@qq.com
 * @LastEditTime: 2022-10-30 08:06:46
 * @FilePath: \v_shop\code\src\components\Login.vue
 * @Description: 登录组件
-->
<template>
  <div class="login_container">
    <!-- 父元素 -->
    <div class="login_box">
      <!-- 头像框 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <div class="login_form">
        <!-- 登录表单 -->
        <el-form
          :model="login_form"
          :rules="loginFormRules"
          ref="loginFormRef"
        >
          <el-form-item prop="username">
            <!-- 用户名 -->
            <el-input type="text" prefix-icon="el-icon-user-solid" v-model="login_form.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <!-- 密码 -->
            <el-input type="password" v-model="login_form.password"  prefix-icon="el-icon-lock"></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <!-- 按钮 -->
            <el-button type="primary" @click="login('loginFormRef')"
              >登录</el-button
            >
            <el-button type="info" @click="resetLoginForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { loginForm } from "../http/api";
import jwt_code from 'jwt-decode'
export default {
  name: "login",
  data() {
    return {
      // 表单绑定
      login_form: {
        username: "",  //用户名绑定
        password: "",  //密码绑定
      },
      // 登录表单校验规则
      loginFormRules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          {
            min: 3,
            max: 6,
            message: "用户名长度在3到6个字符之间",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          {
            min: 6,
            max: 10,
            message: "密码长度在6到10个字符之间",
            trigger: "blur",
          },
        ],
      },
    };
  },

  components: {},

  computed: {},

  mounted() {},

  methods: {
    // 登录
    login(formName) {
      // 校验表单是否验证通过
      this.$refs[formName].validate((valid) => {
        // 是则请求登录接口
        if (valid) {
          loginForm(this.login_form).then((res) => {
            console.log(res);
            // 判断返回状态码是否为200
            if (res.data.meta.status == 200) {
              // 是则提示正确信息
              this.$message.success(res.data.meta.msg);
              // 将token值存储到会话存储
              sessionStorage.setItem("token", res.data.data.token);
              // 解析会话存储中的token值
              const decode=jwt_code(sessionStorage.token)
              // 讲解洗出来的token值存储在vuex中
              this.$store.dispatch('setUser',decode)
              // 跳转到首页
              this.$router.push('/home')
              // 否则提示错误信息
            }else{
              this.$message.error(res.data.meta.msg)
            }
          });
        } else {
          // 否则提示错误信息
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 重置
    resetLoginForm(){3
      this.login_form={
        username:"",
        password:""
      }
    }
  },
  watch: {},
  mixins: [],
};
</script>
<style lang='less' scoped>
.login_container {
  background: #2b4b6b;
  height: 100%;
  position: relative;
  .login_box {
    width: 450px;
    height: 360px;
    background: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avatar_box {
      height: 130px;
      width: 130px;
      border: 1px solid #ccc;
      border-radius: 50%;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      box-shadow: 0 0 10px #ddd;
      background: #ffff;
      padding: 10px;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background: #eee;
      }
    }
  }
}
.login_form {
  width: 100%;
  position: absolute;
  bottom: 25px;
  padding: 0 20px;
  box-sizing: border-box;
  .btns {
    float: right;
  }
}
</style>